package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.AddDistributionDto;
import com.huaxiafinance.management.dto.request.UpdateDistributionDto;
import com.huaxiafinance.management.service.DistributionService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;


/**
 * 分销规则控制器
 */
@Controller("distributionController")
@RequestMapping("/distribution")
public class DistributionController {

  private static final Logger log = Logger.getLogger(DistributionController.class);
  @Autowired
  private DistributionService distributionService;

  @RequestMapping(value = "/getList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getList(Integer state, String name, Integer page, Integer rows) {
    MessageResult result = new MessageResult();
    result.setData(distributionService.getList(state, name, page, rows));
    result.setRows(distributionService.countList(state, name));
    result.setSuccessResult();
    return result.getAsJSON();
  }

  @RequestMapping(value = "/selectOne", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String selectOne(Integer id) {
    MessageResult result = new MessageResult();
    result.setData(distributionService.selectOne(id));
    result.setSuccessResult();
    return result.getAsJSON();
  }


  @RequestMapping(value = "/queryGradeList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryGradeList() {
    MessageResult result = new MessageResult();
    result.setData(distributionService.queryList());
    result.setSuccessResult();
    return result.getAsJSON();
  }


  @RequestMapping(value = "/addOne", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String addOne(@RequestBody @Valid AddDistributionDto dto, BindingResult bindingResult) {
    MessageResult result = new MessageResult();
    if (bindingResult.hasErrors()) {
      result.setFailureResult(bindingResult);
      return result.getAsJSON();
    }
    distributionService.addOne(dto);
    result.setSuccessResult();
    return result.getAsJSON();
  }


  @RequestMapping(value = "/updateOne", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateOne(@RequestBody @Valid UpdateDistributionDto dto, BindingResult bindingResult) {
    MessageResult result = new MessageResult();
    if (bindingResult.hasErrors()) {
      result.setFailureResult(bindingResult);
      return result.getAsJSON();
    }
    distributionService.updateOne(dto);
    result.setSuccessResult();
    return result.getAsJSON();
  }

  @RequestMapping(value = "/updateState", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateState(Integer state, Integer id) {
    MessageResult result = distributionService.updateState(state, id);
    return result.getAsJSON();
  }


}
